{include file="common/head"/}
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>储值卡添加</legend>
    </fieldset>
    <form class="layui-form layui-form-pane">
        <input type="hidden" name="id" value="{$list.id}">
        <div class="layui-form-item">
            <label class="layui-form-label">名称</label>
            <div class="layui-input-4">
                <input type="text" maxlength="200" name="title" value="{$list.title}" lay-verify="required" placeholder="请输入储值卡名称"
                    class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">面值</label>
            <div class="layui-input-4">
                <input type="number" maxlength="200" name="money" value="{$list.money}" lay-verify="required" placeholder="请输入储值卡面值"
                    class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">购买储值卡的价格</div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">返还金额</label>
            <div class="layui-input-4">
                <input type="number" maxlength="200" name="back_money" value="{$list.back_money}" lay-verify="required" placeholder="储值卡返还金额"
                    class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">购买储值卡赠送的金额</div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">截止日期</label>
            <div class="layui-input-4">
                <input type="text" maxlength="200" name="endtime" value="{$list.endtime}" id="start_time" lay-verify="required"
                    placeholder="不设置截止日期或者截止日期为0为无期限" class="layui-input">
            </div>
            <div class="layui-form-mid layui-word-aux">储值卡有效日期,单位：天</div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">背景图</label>
            <input type="hidden" lay-verify="img" name="img" id="logo3" value="{$list.img}">
            <div class="layui-input-block">
                <div class="layui-upload">
                    <button type="button" class="layui-btn layui-btn-primary" id="logoBtn3"><i
                            class="icon icon-upload3"></i>点击上传</button>
                    <div class="layui-upload-list">
                        <img class="layui-upload-img" id="SLogo3" src="{$list.img}">
                        <p id="demoText2"></p>
                    </div>
                </div>
            </div>
        </div>
        
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-4">
                <input type="radio" name="status" value="0" title="正常" {if $list.status==0}checked{/if}>
                <input type="radio" name="status" value="1" title="下架" {if $list.status==1}checked{/if}>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">发放数量</label>
            <div class="layui-input-4">
                <input type="number" maxlength=" 200" name="number" value="{$list.number}" lay-verify="required"
                    placeholder="请输入发放数量" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">活动领取</label>
            <div class="layui-input-4">
                <input type="radio" name="is_activity" value="0" title="否" {if $list.is_activity==0}checked{/if}>
                <input type="radio" name="is_activity" value="1" title="是" {if $list.is_activity==1}checked{/if}>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button type="button" class="layui-btn" id="submit" lay-submit=""
                    lay-filter="sys">{:lang('submit')}</button>
            </div>
        </div>
    </form>
</div>
{include file="common/foot"/}
<script>

    layui.use(['form', 'layer', 'upload', 'laydate'], function () {
        var form = layui.form, layer = layui.layer, upload = layui.upload, $ = layui.jquery;
        let laydate = layui.laydate;
        form.verify({
            img: function (value) {
                if (value == '') {
                    return '请上传背景图！';
                }
            },
            

        });
        //提交监听
 
        var uploadInst2 = upload.render({
            elem: '#logoBtn3'
            , url: '{:url("UpFiles/upload")}',
            accept: 'images',
            acceptMime: 'image/*'
            , size: 0
            , multiple: true
            , number: 1
            , before: function (obj) {
                obj.preview(function (index, file, result) {
                    $('#SLogo3').attr('src', result); //图片链接（base64）
                });
            }
            , done: function (res) {
                if (res.errorCode == 8888) {
                    $('#logo3').val(res.url);
                } else {
                    $('#SLogo3').attr('src', '');
                    layer.msg(res.msg, { time: 2000, icon: 2 });
                }
            }
        });
        
        form.on('submit(sys)', function (data) {
            $("#submit").attr({ disabled: "disabled" });
            loading = layer.load(1, { shade: [0.1, '#fff'] });
            let user = data.field
            let time = data.field.endtime
            if (time == null || !time) {
                data.field.endtime = 0
            }else{
                data.field.endtime = Math.ceil(new Date()/1000)+data.field.endtime*24*3600
            }
            
            $.post("{:url('edit')}", data.field, function (res) {
                layer.close(loading);
                if (res.errorCode == 8888) {
                    layer.msg(res.msg, { icon: 1, time: 1000 }, function () {
                        location.href = res.url;
                    });
                } else {
                    $("#submit").removeAttr("disabled");
                    layer.msg(res.msg, { icon: 2, time: 1000 });
                }
            });
        })
    })


</script>


</body>

</html>